package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpCollectProductSkuProValue;
import com.erp.gather.domain.bo.ErpCollectProductSkuProValueBo;
import com.erp.gather.domain.vo.ErpCollectProductSkuProValueVo;
import com.erp.gather.mapper.ErpCollectProductSkuProValueMapper;
import com.erp.gather.service.IErpCollectProductSkuProValueService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 采集商品SKU属性值Service业务层处理
 *
 * @author erp
 * @date 2023-03-07
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductSkuProValueServiceImpl implements IErpCollectProductSkuProValueService {

	private final ErpCollectProductSkuProValueMapper baseMapper;

	/**
	 * 查询采集商品SKU属性值
	 */
	@Override
	public ErpCollectProductSkuProValueVo queryById(Long id) {
		return baseMapper.selectVoById(id);
	}

	/**
	 * 查询采集商品SKU属性值列表
	 */
	@Override
	public TableDataInfo<ErpCollectProductSkuProValueVo> queryPageList(ErpCollectProductSkuProValueBo bo,
																	   PageQuery pageQuery) {
		LambdaQueryWrapper<ErpCollectProductSkuProValue> lqw = buildQueryWrapper(bo);
		Page<ErpCollectProductSkuProValueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	/**
	 * 查询采集商品SKU属性值列表
	 */
	@Override
	public List<ErpCollectProductSkuProValueVo> queryList(ErpCollectProductSkuProValueBo bo) {
		LambdaQueryWrapper<ErpCollectProductSkuProValue> lqw = buildQueryWrapper(bo);
		return baseMapper.selectVoList(lqw);
	}

	private LambdaQueryWrapper<ErpCollectProductSkuProValue> buildQueryWrapper(ErpCollectProductSkuProValueBo bo) {
		LambdaQueryWrapper<ErpCollectProductSkuProValue> lqw = Wrappers.lambdaQuery();
		lqw.eq(StringUtils.isNotBlank(bo.getPropertyValue()), ErpCollectProductSkuProValue::getPropertyValue,
				bo.getPropertyValue());
		return lqw;
	}

	/**
	 * 新增采集商品SKU属性值
	 */
	@Override
	public Boolean insertByBo(ErpCollectProductSkuProValueBo bo) {
		ErpCollectProductSkuProValue add = BeanUtil.toBean(bo, ErpCollectProductSkuProValue.class);
		validEntityBeforeSave(add);

		// 获取
		ErpCollectProductSkuProValue proValue = baseMapper
				.selectOne(new LambdaQueryWrapper<ErpCollectProductSkuProValue>()
						.eq(ErpCollectProductSkuProValue::getPropertyValue, bo.getPropertyValue()).last("limit 1"));
		if (proValue == null) {
			boolean flag = baseMapper.insert(add) > 0;
			if (flag) {
				bo.setId(add.getId());
			}
			return flag;
		} else {
			bo.setId(proValue.getId());
			return true;
		}
	}

	/**
	 * 修改采集商品SKU属性值
	 */
	@Override
	public Boolean updateByBo(ErpCollectProductSkuProValueBo bo) {
		ErpCollectProductSkuProValue update = BeanUtil.toBean(bo, ErpCollectProductSkuProValue.class);
		validEntityBeforeSave(update);
		return baseMapper.updateById(update) > 0;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(ErpCollectProductSkuProValue entity) {
		// TODO 做一些数据校验,如唯一约束
	}

	/**
	 * 批量删除采集商品SKU属性值
	 */
	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			// TODO 做一些业务上的校验,判断是否需要校验
		}
		return baseMapper.deleteBatchIds(ids) > 0;
	}
}
